package com.hspedu.jdbc.datasource;

import com.alibaba.druid.pool.DruidDataSourceFactory;
import com.hspedu.jdbc.utils.JDBCUtilsByDruid;
import org.junit.Test;

import javax.sql.DataSource;
import java.io.FileInputStream;
import java.sql.Connection;
import java.util.Properties;

/**
 * @author 谷其昊
 * @version 1.0
 * @date 2022/4/10
 * @time 20:45
 */
public class Druid_ {

    /**
     * 手动获取连接
     * @throws Exception
     */
    @Test
    public void testDruid() throws Exception {
        Properties properties = new Properties();
        properties.load(new FileInputStream("D:\\workspace_idea\\java0\\hspedu\\chapter25\\src\\druid.properties"));
        DataSource dataSource = DruidDataSourceFactory.createDataSource(properties);
        Connection connection = dataSource.getConnection();
        System.out.println("获取到连接");
        if (connection != null) {
            connection.close();
        }
    }

    /**
     * 通过 util 类来获取连接
     * @throws Exception
     */
    @Test
    public void testDruid2() throws Exception {
        Connection connection = JDBCUtilsByDruid.getConnection();
        System.out.println("connection = " + connection);
        if (connection != null){
            connection.close();
        }
    }
}
